<%@ page import="com.lyc.product.bean.Product" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/12/10
  Time: 12:51
  To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>商品详情</title>
</head>
<body>
<c:choose>
    <c:when test="${ empty sessionScope.userAccount}">
        <a href="${pageContext.request.contextPath}/html/login.jsp"> 登录|</a>
        <a href="${pageContext.request.contextPath}/html/register.html"> 注册</a>
    </c:when>
    <c:otherwise>
        <div>欢迎${sessionScope.userAccount.nickName} 进入书城
            <a href="${pageContext.request.contextPath}/bookInfo?methodName=queryMyCart&uid=${sessionScope.userAccount.id}">购物车</a>

        </div>
        <a href="${pageContext.request.contextPath}/js/Logout.jsp">退出</a>
    </c:otherwise>
</c:choose>

<div class="border" style="width: 480px; height: 300px; border: 1px solid rosybrown ;margin: 0px auto; overflow: hidden" >
    <div class="bgImg" style="float: left;) ">
        <img  width="250px" height="300px" src="${pageContext.request.contextPath}/${requestScope.bookInfo.productImgurl}" alt="图片无法加载">
    </div>
    <div class="wenzi" style="float: left;margin-left: 20px">
        <br>
        书籍名称：${requestScope.bookInfo.productName}
        <br>
        作者：${requestScope.bookInfo.author}
        <br>
        价格：${requestScope.bookInfo.price}
        <br>
        折扣：${requestScope.bookInfo.discount}
        <br>
        出版社：${requestScope.bookInfo.press}
        <br>
        出版时间：${requestScope.bookInfo.publicationTime}
        <br>
        <input type="text" id="numbers" name="buyNumbers"  max-numbers="${requestScope.bookInfo.numbers}" value="1" style="width: 40px">
        <button type="button" id="btnCart">加入购物车</button>
    </div>
</div>
<script type="text/javascript">
    window.onload=function () {
        var btnCart=document.getElementById("btnCart");
        btnCart.onclick=function () {
        //判断是否登录
            <c:if test="${empty sessionScope.userAccount}">
                    alert("请先登录哦");
            window.location.href="${pageContext.request.contextPath}/html/login.jsp";
            </c:if>
           var numbers= document.getElementById("numbers").value;
             if (numbers>${requestScope.bookInfo.numbers}) {
                 alert("库存不足");
             }
             if (numbers<=0){
                 alert("数量错误");
            }
            var request=new XMLHttpRequest();
             var url="${pageContext.request.contextPath}/bookInfo";
                request.open("POST",url);
                request.setRequestHeader( "Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
                var param ="methodName=addBookToCart&productId=${requestScope.bookInfo.productId}&buyNumbers="+document.getElementsByName("buyNumbers")[0].value;
                request.send(param);
                request.onreadystatechange=function () {
                    if (request.readyState==4){
                        if (request.status==200 || request.status==302) {
                            alert(request.response);
                        }
                    }
                }
        }

    }
</script>
</body>
</html>
